package com.csci;

import java.util.Random;

public class Lattice {

	private final int n;
	int [][] matrix;
	Random rand;
	int numberOfPos = 0;
	int numberOfNeg = 0;
	
	public Lattice(int n){
		this.n = n;
		matrix = new int[n][n];
		rand = new Random();
	}
	
	public void initializeLattice() {
		for(int row = 0; row < n; row++){
			for(int col = 0; col < n; col++){
				if(rand.nextFloat()<.5){
					matrix[row][col] = -1;
					numberOfNeg++;
				}
				else{
					matrix[row][col] = 1;
					numberOfPos++;
				}
			}
		}
	}
	
	public char convertElementToSymbol(int matrixElement){
		if(matrixElement == -1){
			return '-';
		}
		else
			return '+';
	}
	
	public int sumAdjacentLatticeElements(int[][] matrix, int row, int col) {
		int sum = 0;
		if(checkCoupling(matrix, row, col, row, getLeftNeighbor(col))){
			sum++;
		}
		if(checkCoupling(matrix, row, col, row, getRightNeighbor(col))){
			sum++;
		}
		if(checkCoupling(matrix, row, col, getTopNeighbor(row), col)){
			sum++;
		}
		if(checkCoupling(matrix, row, col, getBottomNeighbor(row), col)){
			sum++;
		}
		return sum;
	}
	
	public boolean checkCoupling(int [][] matrix, int rowCurrent, int colCurrent, int rowNeighbor, int colNeighbor){
		if(matrix[rowCurrent][colCurrent] == matrix[rowNeighbor][colNeighbor]){
			return true;
		}
		else
			return false;
	}
	
	public int getLeftNeighbor(int currentCol){
		if(currentCol == 0){
			return (n-1);
		}
		else{
			return currentCol-1;
		}
	}
	
	public int getRightNeighbor(int currentCol) {
		if(currentCol == (n-1))
			return 0;
		else
			return currentCol+1;
	}
	
	public int getTopNeighbor(int currentRow){
		if (currentRow == 0){
			return n-1;
		}
		else{
			return (currentRow-1);
		}
	}	
	
	public int getBottomNeighbor(int currentRow){
		if(currentRow == (n-1)){
			return 0;
		}
		else{
			return currentRow+1;
		}
	}
	
}
